// 导入Promise
import {
	request
} from '../../request/index.js'

Page({
	data: {
		// 左侧菜单数据
		leftMenuList:[],
		// 右侧商品数据
		rightContent:[],
		//激活选中
		currentIndex:0,
		//滚动为0
		scrollTop:0
	},
	// 接口返回数据，临时资源不做处理不需要放在data里面
	Cates:[],
	
	onLoad: function (options) {
		const Category = wx.getStorageSync("Category")
		// console.log(Category);
		
		if(!Category){
			this.getCates()
		}else{
			if( Date.now() - Category.timer > 1000 * 10){
				this.getCates()
			}else{
				this.Cates = Category.data
				let leftMenuList = this.Cates.map(v =>  v.cat_name)	
				let rightContent = this.Cates[0].children
				this.setData({
					leftMenuList,
					rightContent
				})
			}
		}
	},
	// 分类数据
	async getCates(){
		const res = await request({url: '/categories'})
		this.Cates = res.data.message;
		// console.log(this.Cates);
			
		//把上面的Cates存入,因为有数据了
		wx.setStorageSync("Category", {
			timer:Date.now(),
			data:this.Cates
		})
			
		// 遍历数组进行映射，构建一个新数组
		let leftMenuList = this.Cates.map(v =>  v.cat_name)	
		// console.log(leftMenuList);
		let rightContent = this.Cates[0].children
			
		this.setData({
			leftMenuList,
			rightContent
		})
		
	},
	
	//点击切换样式
	handleItemTap(e){
		
		const {index} = e.currentTarget.dataset
		let rightContent = this.Cates[index].children
		
		this.setData({
			currentIndex : index,
			rightContent,
			scrollTop:0
		})
		
		
		
	}
	
})